﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Threading;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class PlayHelper
    {
        public PlayHelper(ScriptParser parser)
        {
            this.parser = parser;
            this.IsAutoRunning = false;
            this.Timer = new DispatcherTimer();
            this.Timer.Tick += new EventHandler(Timer_Tick);

            if (Helper.GraphicBase != null)
            {
                this.AutoBB = (Helper.GraphicBase.GalUIFrame.GamingFrame as System.Windows.FrameworkElement).FindName(GameFrameLibrary.Strings.AutoBB) as GameFrameLibrary.BoolenButton;
                this.SkipBB = (Helper.GraphicBase.GalUIFrame.GamingFrame as System.Windows.FrameworkElement).FindName(GameFrameLibrary.Strings.SkipBB) as GameFrameLibrary.BoolenButton;
            }
        }

        private void NextStart()
        {
            if (!(this.parser.CurrentState.Scene is GamingScene))
            {
                this.StopAutoPlay();
                this.StopSkipMode();
                return;
            }

            if (this.parser.CurrentState.GamingScene.Dialogue.Text == null)
                this.parser.CurrentState.GamingScene.Dialogue.Text = "";

            if (this.parser.CurrentState.GamingScene != null)
            {
                if (this.parser.CurrentState.GamingScene.Selector != null)
                {
                    if (Helper.Parser.SelectorParser.IsJustExecuteOption)
                    {
                        Helper.Parser.SelectorParser.IsJustExecuteOption = false;
                    }
                    else
                    {
                        this.StopAutoPlay();
                        this.StopSkipMode();
                        return;
                    }
                }


                if (this.IsAutoRunning)
                {
                    var dialogueTime = TimeSpan.FromMilliseconds(this.parser.CurrentState.GamingScene.Dialogue.Text.Count() * 50 + 2000);
                    this.Timer.Interval = this.BasicWaitingTime + dialogueTime;
                    this.Timer.Start();
                }
                else if (this.IsSkipMode)
                {
                    var log = this.parser.CurrentState.Scene as FunctionModel.IReaderLogable;
                    if (Helper.GameConfig.WindowConfig.IsStopNotRead)
                    {
                        if (log.IsRead)
                        {
                            var dialogueTime = TimeSpan.FromMilliseconds(600);
                            this.Timer.Interval = this.BasicWaitingTime + dialogueTime;
                            this.Timer.Start();
                        }
                        else
                        {
                            this.StopSkipMode();
                        }
                    }
                    else
                    {
                        var dialogueTime = TimeSpan.FromMilliseconds(600);
                        this.Timer.Interval = this.BasicWaitingTime + dialogueTime;
                        this.Timer.Start();
                    }
                }
                else return;

            }

        }

        public void SwitchAutoPlay()
        {
            if (this.IsSkipMode)
            {
                this.StopSkipMode();
                this.SkipBB.IsChecked = false;
                return;
            }

            if (this.IsAutoRunning)
            {
                this.StopAutoPlay();
                this.AutoBB.IsChecked = false;
                return;
            }

            this.IsAutoRunning = true;

            this.AutoBB.IsChecked = true;

            this.BasicWaitingTime = TimeSpan.FromSeconds(GameFrameLibrary.Strings.AutoBasicWatingTime);

            this.Timer_Tick(this, EventArgs.Empty);
        }

        public void SwitchSkipMode()
        {
            if (this.IsAutoRunning)
            {
                this.StopAutoPlay();
                this.AutoBB.IsChecked = false;
                return;
            }
            if (this.IsSkipMode)
            {
                this.StopSkipMode();
                this.SkipBB.IsChecked = false;
                return;
            }

            this.IsSkipMode = true;

            this.SkipBB.IsChecked = true;

            this.BasicWaitingTime = TimeSpan.FromMilliseconds(1);

            this.Timer_Tick(this, EventArgs.Empty);
        }

        public void StopAutoPlay()
        {
            this.IsAutoRunning = false;

            this.Timer.Stop();

            this.AutoBB.IsChecked = false;
        }

        public void StopSkipMode()
        {
            this.IsSkipMode = false;

            this.Timer.Stop();

            this.SkipBB.IsChecked = false;
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            if (this.parser.IsParsing)
                return;
            this.parser.StartCurrent();
            this.NextStart();
        }

        private DispatcherTimer Timer { get; set; }

        public TimeSpan BasicWaitingTime { get; set; }

        public bool IsAutoRunning { get; set; }

        private ScriptParser parser;

        public bool IsSkipMode { get; set; }

        public GameFrameLibrary.BoolenButton AutoBB { get; set; }

        public GameFrameLibrary.BoolenButton SkipBB { get; set; }
    }
}
